package com.example.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Date 2021/7/11
 * @Created by Jonathan
 * <p>
 * 这个Bean被注入就有问题了：
 * Description:
 * The bean 'applicationRunnerService' could not be injected
 * as a 'com.example.service.ApplicationRunnerService'
 * because it is a JDK dynamic proxy that implements:
 * org.springframework.boot.ApplicationRunner
 * <p>
 * Action:
 * Consider injecting the bean as one of its interfaces or
 * forcing the use of CGLib-based proxies by
 * setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.
 */
@Slf4j
@Service
public class ApplicationRunnerService implements ApplicationRunner {

    /*@Resource
    private ApplicationRunnerService applicationRunnerService;*/


    public void sayHello() {
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleAtFixedRate(() -> {
            log.info("hello world");
        }, 5, 100, TimeUnit.SECONDS);
    }

    @Async
    public void test() {
        log.info("test method...");
        /*applicationRunnerService.test1();*/
    }

    /*@Async
    public void test1() {
        log.info("test-1 method");
    }*/

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        sayHello();
    }
}
